package com.sunlands.deskmate.message.handler.storager.interceptor.impl;

import com.sunlands.deskmate.message.entity.Messages;
import com.sunlands.deskmate.message.enums.MessageType;
import com.sunlands.deskmate.channel.WebSocketServerHandler;
import com.sunlands.deskmate.message.handler.storager.interceptor.Interceptor;
import com.sunlands.deskmate.service.DialogRecordServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Set;

/**
 * 私聊、群、房间会话建立销毁处理
 * 退出房间、进入房间
 * 退出群聊、进入群聊
 * 退出私聊、进入私聊
 */
@Order(1000)
@Slf4j
@Component
public class DialogEnterOrQuitMessageInterceptor implements Interceptor {


    @Autowired
    private DialogRecordServiceImpl dialogRecordService;

    @Override
    public boolean interceptor(Messages msgEntity) {
        log.info("DialogEnterOrQuitMessageInterceptor msgEntity = {}", msgEntity);
        if (MessageType.ENTER_GROUP.getType().equals(msgEntity.getType())
                || MessageType.ENTER_ROOM.getType().equals(msgEntity.getType())
                || MessageType.ENTER_PRIVATE_CHAT.getType().equals(msgEntity.getType())) {
            String key = generateKey(msgEntity);
            log.debug("会话增加用户 enter key = {}", key);
            dialogRecordService.addOnlineSet(key, msgEntity);
        }

        if (MessageType.QUIT_GROUP.getType().equals(msgEntity.getType())
                || MessageType.QUIT_ROOM.getType().equals(msgEntity.getType())
                || MessageType.QUIT_PRIVATE_CHAT.getType().equals(msgEntity.getType())) {
            String key = generateKey(msgEntity);
            Set<Long> onlineSet = dialogRecordService.refreshMembers(key);
            if (onlineSet != null) {
                log.debug("会话移除用户 enter key = {}", key);
                dialogRecordService.remove(key, msgEntity);
            } else {
                log.warn("会话已经不存在，不需要从会话中移除用户, key={}", key);
            }
        }
        return true;
    }

    private String generateKey(Messages msgEntity) {
        if (MessageType.PRIVATE_CHAT.getType().equals(msgEntity.getType())
                || MessageType.SHARE_GROUP_TO_PRIVATE.getType().equals(msgEntity.getType())
                || MessageType.SHARE_ROOM_TO_PRIVATE.getType().equals(msgEntity.getType())
                || MessageType.ENTER_PRIVATE_CHAT.getType().equals(msgEntity.getType())
                || MessageType.QUIT_PRIVATE_CHAT.getType().equals(msgEntity.getType())
                || MessageType.SHARE_DIARY_TO_PRIVATE.getType().equals(msgEntity.getType())
                || MessageType.SHARE_DIARY_COMMENT_TO_PRIVATE.getType().equals(msgEntity.getType())) {
            return getPrivateChatRoomKey(msgEntity);
        } else {
            return WebSocketServerHandler.getRoomKey(msgEntity.getType(), msgEntity.getToId());
        }
    }


    public static String getPrivateChatRoomKey(Messages messages) {
        return WebSocketServerHandler.getRoomKey(messages.getType(), String.valueOf(Math.max(Integer.parseInt(messages.getToId()), Integer.parseInt(messages.getFromUserId()))));
    }


}